package client;

import java.math.BigInteger;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.ws.BindingProvider;
import javax.xml.ws.Endpoint;
import javax.xml.ws.handler.Handler;
import javax.xml.ws.soap.AddressingFeature;

import client.handler.ClientHandler;
import org.example.informacionestudiante.*;
import org.example.schemaestudiante.Doc;
import org.example.schemaestudiante.Est;
import org.example.schemaestudiante.PaisDoc;
import org.example.schemaestudiante.TipoDoc;

/**
 * Ejemplo de uso de WS-Addressing Sincronico y Asincronico
 * 
 */
public class ClientInformacionEstudiante {

	private static final String SERVER_ADDRESS = "http://localhost:8080/ejercicio3/InformacionEstudiante";

	private static final String REPLY_TO_ADDRESS = "http://localhost:8080/ejercicio3/CallBackDinamic";

	private InformacionEstudiante proxy;

	public ClientInformacionEstudiante() throws MalformedURLException {
		InformacionEstudiante_Service service = new InformacionEstudiante_Service();
		// create the proxy with addressing enabled
		proxy = service.getPort(InformacionEstudiante.class,  new AddressingFeature());
		
		// explicitly specify the server address
		// this is useful when you want to intercept the request
		BindingProvider provider = (BindingProvider) proxy;
		provider.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, SERVER_ADDRESS);
	}

	public static void main(String[] args) throws Exception {
		ClientInformacionEstudiante client = new ClientInformacionEstudiante();

//		System.out.println("--> Sincronico WS-Addressing");
//		client.testAnonymousReply();

		System.out.println("--> Asincronico WS-Addressing");
		client.testNonAnonymousReply();
	}

	/**
	 * The ReplyTo address is not specified. This is equivalent to a synchronous
	 * scenario where the response is immediately available.
	 */
	public void testAnonymousReply() {

		
	     Doc documento = new Doc();     
	     documento.setPais(PaisDoc.UY);
	     documento.setNumero(BigInteger.valueOf(38141351));
	     documento.setTipo(TipoDoc.CI);
	     Est estudiante = proxy.obtenerInfoEstudiante(documento);
	     System.out.println("El resultado obtenido en el cliente es: "+estudiante.getApellidos());
	}
	
	


	/**
	 * The ReplyTo address is set to a different web service (CallBackDinamic)
	 * maintained by the client.
	 */
	public void testNonAnonymousReply() {
		System.out.println("Starting the CallBackDinamic endpoint...");
		//Endpoint endpoint = Endpoint.publish(REPLY_TO_ADDRESS,new CallBackDinamic());
		
		/* handler for setting the ReplyTo address */
		BindingProvider provider = (BindingProvider) proxy;
		@SuppressWarnings("rawtypes")
		List<Handler> handlerChain = new ArrayList<Handler>();
		handlerChain.add(new ClientHandler());
		provider.getBinding().setHandlerChain(handlerChain);

		// pass the replyTo address to the handler
		provider.getRequestContext().put("ReplyTo",REPLY_TO_ADDRESS);
	   
		// this request is equivalent to an one-way call
		// the response is expected in the CallBackDinamic service
	    Doc documento = new Doc();     
	    documento.setPais(PaisDoc.UY);
	    documento.setNumero(BigInteger.valueOf(38141351));
	    documento.setTipo(TipoDoc.CI);
	    Est estudiante = proxy.obtenerInfoEstudiante(documento);
	    
		try {
			Thread.sleep(300);
		} catch (InterruptedException ex) {
			throw new RuntimeException(ex);
		}
		
		System.out.println("Stopping the CallBackDinamic endpoint...");
		//endpoint.stop();
	}

}
   
